<?php
/*
 * @Author: Terry
 * @Date: 2025-03-28 10:57:44
 * @LastEditors: Terry
 * @LastEditTime: 2025-03-28 16:17:44
 * @Description: file content
 * @FilePath: \yyd_api\app\controller\user\v1\Common.php
 */
declare (strict_types = 1);

namespace app\controller\user\v1;

use app\BaseController;
use think\facade\Db;
use think\facade\Validate;
use think\facade\Filesystem;
class Common extends BaseController{
       /**
     * 上传文件接口
     * @return void
     */
    public function uploadImg()
    {
        try {
            $uuid = $this->request->uuid;
            $data = $this->request->data;
            $path = $data['path'] ?? 'images/uploads/'.date('Ymd');
            // 获取上传文件
            $file = $this->request->file('file');
            if (empty($file)) {
                return result(FAIL, '没有上传文件');
            }
            // 确保目录存在
            if (!is_dir($path)) {
                mkdir($path, 0755, true);
            }
            try {
                //只能上传图片，大小不能超过2M
                validate(['file'=>'fileSize:2048|fileExt:jpg,jpeg,png,gif'])->check([$file]);
                $savename = Filesystem::disk('public')->putFile( $path, $file);
                $domain = $this->request->domain();
                $filePath = $domain . "/storage/" . $savename;
                return result(SUCCESS, '上传成功', ['url' => $filePath]);
            } catch (\think\exception\ValidateException $e) {
                return result(FAIL, $e->getMessage());
            }
        } catch (\Exception $e) {
            return result(FAIL, '系统错误：' . $e->getMessage());
        }
    }

} 